package com.dtz.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.dtz.order.feign.PaymentFeign;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class OrderController {

	@Autowired
	private PaymentFeign paymentFeign;
	
	@GetMapping("/consumer/order/ok/{id}")
	public String ok(@PathVariable("id") Long id) {
		String result = paymentFeign.paymentOk(id);
		log.info("result = " + result);
		return result;
	}
	
	@GetMapping("/consumer/order/timeout/{id}")
	@HystrixCommand(fallbackMethod = "timeoutHandler", commandProperties = {
			@HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "7000")
	})
	public String timeout(@PathVariable("id") Long id) {
		String result = paymentFeign.timeout(id);
		log.info("result = " + result);
		return result;
	}
	
	public String timeoutHandler(Long id) {
		return "订单服务调用支付服务超时，id = " + id;
	}
	
}
